
package org.utn.edu.tsb.parser_xml.Interface;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class Persistence {

    private static String lastPath=null;
    private static String[] lastFiles=null;

    public static void savePath(String path) {
        Persistence.lastPath = path;        
        try
           {
             FileOutputStream ostream = new FileOutputStream("preferences.cfg");
             ObjectOutputStream p = new ObjectOutputStream(ostream);

             p.writeObject(new Preferences(Persistence.lastPath, Persistence.lastFiles));

             p.flush();
             ostream.close();
           }
           catch ( Exception e )
           {

           }
    }

    public static void addFile(String file) {
        try {
            getLastFiles();
        } catch (Exception e) {
        }
        if (lastFiles == null) {
            lastFiles = new String[4];
        }
        for (int i = lastFiles.length-1; i >= 0; i--) {
            if(i==0){
                lastFiles[0]=file;
            }else{
                lastFiles[i] = lastFiles[i - 1];
            }
        }

        try
           {
             FileOutputStream ostream = new FileOutputStream("preferences.cfg");
             ObjectOutputStream p = new ObjectOutputStream(ostream);

             p.writeObject(new Preferences(Persistence.lastPath, Persistence.lastFiles));

             p.flush();
             ostream.close();
           }
           catch ( Exception e )
           {

           }
    }

    public static String[] getLastFiles() {
        try
           {
                 FileInputStream istream = new FileInputStream("preferences.cfg");
                 ObjectInputStream p = new ObjectInputStream(istream);

                 Persistence.lastFiles = (( Preferences ) p.readObject()).getLastFiles();

                 p.close();
                 istream.close();
           }
           catch (Exception e)
           {

           }
        return Persistence.lastFiles;
    }

    public static String getLastPath(){
        try
           {
                 FileInputStream istream = new FileInputStream("preferences.cfg");
                 ObjectInputStream p = new ObjectInputStream(istream);

                 Persistence.lastPath = (( Preferences ) p.readObject()).getLastPath();

                 p.close();
                 istream.close();
           }
           catch (Exception e){}
        return Persistence.lastPath;
    }    
}

class Preferences implements Serializable {

    private String lastPath;
    private String[] lastFiles;

    public Preferences(String lastPath, String[] lastFiles) {
        this.lastPath = lastPath;
        this.lastFiles = lastFiles;
    }

    String[] getLastFiles() {
        return lastFiles;
    }

    String getLastPath() {
        return lastPath;
    }
}
